/**
 * @file    drv_spi.h
 * @author  yhy
 * @brief   芯必达IM94x系列芯片, spi抽象层
 * @version 1.03
 * @date    2025-07-08
 *
 * @copyright Copyright (c) 2025
 *
 * @details
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-08     yhy          the first version V1.00
 * 2025-07-11     yhy          所有函数和变量名修改为snf_开头 V1.01
 * 2025-07-18     yhy          完善spi统一回调函数, 实现了spi-dma循环发送 V1.02
 * 2025-07-24     yhy          IMMO修改了spi-dma发送的库函数，实现啦真正的SPI-DMA循环发送 V1.03
 *
 */
#ifndef __DRV_SPI_H__
#define __DRV_SPI_H__

#include "common_config.h"

#include <stdbool.h>
#include <stdint.h>

#define SPI_INSTANCE_MAXSIZE    (4 + 2 + 1) /* SPI实例数量 (SPI0-4、SPI1-2、SPI2-1) */
#define SPI_HW_INSTANCE_MAXSIZE (3)         /* 硬件SPI实例数量 */

/* SPI回调函数 */
typedef void (*spi_callback_f)(uint32_t instance, void *param);

/**
 * @brief SPI实例
 *
 */
typedef enum
{
    SPI_INSTANCE_SPI0_0 = 0U, /* MOSI: PD16, SCK: PD15, CS: PB5, MISO: PB4 FUN4 */
    SPI_INSTANCE_SPI0_1 = 1U, /* MOSI: PB3, SCK: PB2, CS: PB0, MISO: PB1 FUN3 */
    SPI_INSTANCE_SPI0_2 = 2U, /* MOSI: PA1, SCK: PA0, CS: PE6, MISO: PE2 FUN4 */
    SPI_INSTANCE_SPI0_3 = 3U, /* MOSI: PE1, SCK: PE0, CS: PA10, MISO: PA11 FUN4 */
    SPI_INSTANCE_SPI1_0 = 4U, /* MOSI: PD1, SCK: PD0, CS: PD3, MISO: PD2 FUN3 */
    SPI_INSTANCE_SPI1_1 = 5U, /* MOSI: PC7, SCK: PC6, CS: PA12, MISO: PA13 FUN3 */
    SPI_INSTANCE_SPI2_0 = 6U, /* MOSI: PC0, SCK: PC15, CS: PC14, MISO: PC1 FUN4 */
    SPI_INSTANCE_MAX
} snf_spi_instance_e;

/**
 * @brief 数据宽度
 *
 */
typedef enum
{
    SPI_DATA_SIZE_8BIT  = 0U,
    SPI_DATA_SIZE_16BIT = 1U,
    SPI_DATA_SIZE_32BIT = 2U,
    SPI_DATA_SIZE_MAX,
} snf_spi_data_size_e;

/**
 * @brief SPI 初始化配置
 *
 */
typedef struct
{
    snf_spi_instance_e  instance;       /*!< SPI 实例 */
    snf_spi_data_size_e data_size;      /*!< 数据宽度 */
    spi_callback_f      spi_callback;   /*!< SPI 回调函数 */
    void               *callback_param; /*!< 回调参数 */
} snf_spi_config_t;

result_e snf_drv_spi_init(const snf_spi_config_t *spi_config);
result_e snf_drv_spi_transfer(snf_spi_instance_e instance, uint8_t *tx_buf, uint8_t *rx_buf,
                              uint32_t buf_size);

#endif
